//
//  TopCollectionViewController.m
//  OurMenu
//
//  Created by lanou3g on 15/10/10.
//  Copyright © 2015年 尹坤. All rights reserved.
//

#import "TopCollectionViewController.h"

//static NSInteger integ = 0;
@interface TopCollectionViewController ()

@end

@implementation TopCollectionViewController
{
    
        NSString *urlStr;
        DownLoadDataHelper *down;
        NSString  *NowStr;
        NSInteger number;
        //NSString *str;
        UIView *loadingView;
    //NSMutableArray *dataArray;
    
}

static NSString * const reuseIdentifier = @"Cell";
NSInteger numberr = 10;
NSInteger nub1 = 1;

//-(id)init
//{
//    self = [super initWithCollectionViewLayout:[[TWSpringyFlowLayout alloc] initWithBounceFactor:20]];
//    if (self) {
//        
//    }
//    
//    return self;
//}

- (id)init
{
    self = [super initWithCollectionViewLayout: [[TWSpringyFlowLayout alloc] init]];
    if (self)
    {
        
    }
    return self;
}

-(void)loadView
{
    [super loadView];
    
    //    TWSpringyFlowLayout *layout = [[TWSpringyFlowLayout alloc] initWithBounceFactor:20];
    //    self.collectionView.collectionViewLayout = layout;
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    //    // Background
    //    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"messages_bg_2.png"]];
    //    backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    
    
    
    //    [self.view insertSubview:backgroundImageView belowSubview:self.collectionView];
    // Parallax effect
    UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    interpolationHorizontal.minimumRelativeValue = @-20.0;
    interpolationHorizontal.maximumRelativeValue = @20.0;
    
    UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    interpolationVertical.minimumRelativeValue = @-20.0;
    interpolationVertical.maximumRelativeValue = @20.0;
    //    [backgroundImageView addMotionEffect:interpolationHorizontal];
    //    [backgroundImageView addMotionEffect:interpolationVertical];
    // 注册
    [self.collectionView registerClass:[TopCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //设置背景
    UIImageView *BVImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    BVImage.image = [UIImage imageNamed:@"Shrimp-11.jpg"];
    BVImage.alpha = 0.4;
    [self.view insertSubview:BVImage belowSubview:self.collectionView];
    
    
    //视差
    
    
    [BVImage addMotionEffect:interpolationHorizontal];
    [BVImage addMotionEffect:interpolationVertical];
    
    //配置 collectionView
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
////    TWSpringyFlowLayout *layout = [[TWSpringyFlowLayout alloc] initWithBounceFactor:20];
////    self.collectionView.collectionViewLayout = layout;
//
//    
//    self.view.backgroundColor = [UIColor whiteColor];
////    // Background
////    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"messages_bg_2.png"]];
////    backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//    
//    
//    
//    
////    [self.view insertSubview:backgroundImageView belowSubview:self.collectionView];
//    // Parallax effect
//    UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
//    interpolationHorizontal.minimumRelativeValue = @-20.0;
//    interpolationHorizontal.maximumRelativeValue = @20.0;
//    
//    UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
//    interpolationVertical.minimumRelativeValue = @-20.0;
//    interpolationVertical.maximumRelativeValue = @20.0;
////    [backgroundImageView addMotionEffect:interpolationHorizontal];
////    [backgroundImageView addMotionEffect:interpolationVertical];
//    // 注册
//    [self.collectionView registerClass:[TopCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
//    self.collectionView.delegate = self;
//    self.collectionView.dataSource = self;
//    //设置背景
//    UIImageView *BVImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
//    BVImage.image = [UIImage imageNamed:@"Shrimp-11.jpg"];
//    BVImage.alpha = 0.4;
//    [self.view insertSubview:BVImage belowSubview:self.collectionView];
//   
//    
//    //视差
//    
//    
//    [BVImage addMotionEffect:interpolationHorizontal];
//    [BVImage addMotionEffect:interpolationVertical];
//
//    //配置 collectionView
//    self.collectionView.backgroundColor = [UIColor clearColor];
    
    down = [[DownLoadDataHelper alloc] init];
    
    down.sendDataDelegete = self;
    self.str = @"素菜";
    
    urlStr = [NSString stringWithFormat:@"http://apis.juhe.cn/cook/query?key=40b7c9d3c5b1dd83e8c349431510d7c4&menu=%@&rn=10&pn=1",self.str];
    // ios 9转码
    NowStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    
    [self.collectionView addFooterWithTarget:self action:@selector(dragUp)];
    
    [down downLoadDataWithUrlString:NowStr];
    
  

}

- (NSMutableArray *)dataArrays
{
    if (!_dataArrays) {
        self.dataArrays = [NSMutableArray array];
    }
    return _dataArrays;
}



- (void)sendDataBack:(NSData *)data
{
    NSDictionary *allDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *towDic = [allDic objectForKey:@"result"];
    NSArray *allArray = [towDic objectForKey:@"data"];
     //_dataArray = [NSMutableArray array];
    for (NSDictionary *dic in allArray) {
        DishesMadels *dishes = [[DishesMadels alloc] init];
        [dishes setValuesForKeysWithDictionary:dic];
        [self.dataArrays addObject:dishes];
//        integ = self.dataArrays.count;
    }
    NSLog(@"-----------%ld",(long)self.dataArrays.count);
//    self.collectionView.delegate = self;
//    self.collectionView.dataSource = self;
    [self.collectionView reloadData];
    
}


- (void)dragUp
{
    number = (number++) * 10 + 1;
    NSString *string = [NSString stringWithFormat:@"%ld",number];
    urlStr = [NSString stringWithFormat:@"http://apis.juhe.cn/cook/query?key=40b7c9d3c5b1dd83e8c349431510d7c4&menu=%@&rn=20&pn=%@",self.str,string];
    // ios 9转码
    NowStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [down downLoadDataWithUrlString:NowStr];
    [self.collectionView footerEndRefreshing];
    numberr = (nub1++) * 10;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //NSLog(@"+++++++%ld",self.dataArrays.count);
    return 10;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSDictionary *item = [_dataArrays objectAtIndex:indexPath.row];
    NSLog(@"%@",item);
    NSString *imgURL = [[item valueForKey:@"albums"] lastObject];
    NSLog(@"%@",imgURL);
//
//    
    //imgView.image = [UIImage imageNamed:imgURL];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:@"1"]];
//    NSString *playerName = [item valueForKey:@"title"];
//    //cell.imgView.image = [UIImage imageNamed:@"1"];
//    cell.titleLabel.text = playerName;
//    
    cell.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.2];
//cell.headImage.image = [UIImage imageNamed:@"Shrimp-11.jpg"];
    cell.titleLabel.text = [item valueForKey:@"title"];
    cell.mainMaterialLabel.text = @"红丸子、白丸子、熘丸子、炸丸子、三鲜丸子、四喜丸子、氽丸子、葵花丸子、饹炸丸子、豆腐丸子、红炖肉、白炖肉、松肉、扣肉、烤肉、酱肉、荷叶卤、一品肉、樱桃肉、马牙肉、酱豆腐肉、坛子肉、罐儿肉、元宝肉、福禄肉";
    
    
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGRect cellFramInSuperView = [collectionView convertRect:[collectionView cellForItemAtIndexPath:indexPath].frame toView:[collectionView superview]];

    
    DetailsViewController *deta = [[DetailsViewController alloc] init];
    deta.yOrigin = cellFramInSuperView.origin.y - 60;
    
    NSLog(@"%f",cellFramInSuperView.origin.y - 60);
    
    [self.navigationController pushViewController:deta animated:NO];
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSLog(@"%ld",indexPath.row);
}

#pragma mark - 设置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(self.view.frame.size.width - 20, 120);
}



#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
